package com.doit.doit47;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.serialization.StringDeserializer;

import java.time.Duration;
import java.util.Arrays;
import java.util.Properties;

public class _05_光哥专属 {
    public static void main(String[] args) {
        Properties props = new Properties();
        props.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,MyKafkaConfig.CONSUMER_HOST_NAME);
        props.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        props.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        props.setProperty(ConsumerConfig.GROUP_ID_CONFIG,"guangge");
        //选择配置的
        props.setProperty(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,"true");
//        props.setProperty(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG,"10000");
        //1.代码中有没有设置过从哪里开始读
        //2.再看__consumer_offsets这个topic中有没有记录,如果有记录，就直接从上次消费到的地方开始读
        //3.再去看AUTO_OFFSET_RESET_CONFIG 是earliest 还是latest
        props.setProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,"latest");

        // 创建kafka消费者的对象
        KafkaConsumer<String, String> consumer = new KafkaConsumer<String, String>(props);

//        consumer.subscribe(Arrays.asList("guangge"));
        TopicPartition topicPartition = new TopicPartition("guangge", 2);
        consumer.assign(Arrays.asList(topicPartition));
        consumer.seek(topicPartition,100);


        while (true){
            ConsumerRecords<String, String> poll = consumer.poll(Duration.ofMillis(Long.MAX_VALUE));
            for (ConsumerRecord<String, String> record : poll) {
                String value = record.value();
                System.out.println(record.partition()+","+value);
            }
        }


    }
}
